<?php
namespace app\mobile\controller;
//use Seller\Controller\HomeController;
use think\Db;
use think\Page;

class Interact extends MobileBase
{

    public $user_id = 0;
    public $user = array();

    public function _initialize()
    {
    	$this->action=I('get.action');
	 	if($this->action!='page'){
	 		parent::_initialize();
	 		$this->assign('signPackage',$_SESSION['signPackage']);

	        if (session('?user')) {
	            $user = session('user');
	            $user = M('users')->where("user_id", $user['user_id'])->find();
	            session('user', $user);  //覆盖session 中的 user
	            $this->user = $user;
	            $this->user_id = $user['user_id'];
	            $this->assign('user', $user); //存储用户信息
	        }
	        $nologin = array(
	            'login', 'pop_login', 'do_login', 'logout', 'verify', 'set_pwd', 'finished',
	            'verifyHandle', 'reg', 'send_sms_reg_code', 'find_pwd', 'check_validate_code',
	            'forget_pwd', 'check_captcha', 'check_username', 'send_validate_code', 'express',
	        );
	        if (!$this->user_id && !in_array(ACTION_NAME, $nologin)) {
	            header("location:" . U('Mobile/User/login'));
	            exit;
	        }
        }
        /*//每天增加5票
        $vdata['fvote_give_count']=5;
        $vdata['fupdate_time']=time();
        $fupdate_time=M('vote_remain')->where(array('fvip_key'=>session('user.user_id')))->getField('fupdate_time');
        if($fupdate_time<strtotime(date('Y-m-d 0:0:0'))){
        	M('vote_remain')->where(array('fvip_key'=>session('user.user_id')))->save($vdata);
        }*/
        

    }

    /*
    * 初始化操作
    */
 


	public function vote_view(){
		    $action=I('get.action');
		    $moban_id=I('get.moban');
		    $vote_id=(int)I('get.voteid');
		    $dltoken=I('get.dltoken',$_SESSION['dltoken']);
		
			// 进行分页数据查询 注意page方法的参数的前面部分是当前的页数使用 $_GET[p]获取
			$P=(string)I('get.p','1');
			$order=(string)I('get.order','1');
			if($order=='1'){
				$orderby='fkey';
			}
			if($order=='2'){
				$orderby='fvitem_count';
			}

			

			//$Vote_item = D('Voteitem');//实例化视图
			$list = M('vote_item')->table('tp_vote_item t0,tp_users t1')
			->field('t0.fkey,t0.fvip_key,t0.fvote_key,t0.fvitem_imgs,t0.fvitem_xuanyan,t0.fvitem_count,t0.fvitem_bmtime,t1.nickname')
			->where(array('fvote_key'=>$vote_id))
			->where('t0.fvip_key=t1.user_id')
			->order($orderby.' desc')
			->page($P.',15')
			->select();
			$count = M('vote_item')->where(array('fvote_key'=>$vote_id))->count();// 查询满足要求的总记录数
			$Page  = new Page($count,15);// 实例化分页类 传入总记录数和每页显示的记录数
			$Page->setConfig('prev','上一页');
			$Page->setConfig('next','下一页');
			$show= $Page->show();// 分页显示输出
			$htmlstr='';
			foreach ($list as $key => $value) {
				$wximg=json_decode($value['fvitem_imgs']);
				$wximgstr=implode(';',$wximg);
				$fvitem_count=($value["fvitem_count"]==null)?0:$value["fvitem_count"];
				if(is_base64($value['nickname']) ==true){
					$fvip_webcnickname=base64_decode($value["nickname"]);

				}else{
					$fvip_webcnickname=$value["nickname"];
				}
				if(is_base64($value['fvitem_xuanyan']) ==true){
					$fvitem_xuanyan=base64_decode($value["fvitem_xuanyan"]);
					
				}else{
					$fvitem_xuanyan=$value["fvitem_xuanyan"];
				}
				if($order=='2'){
					$htmlstr.='<li class="item"><div class="box"><i>'.(((int)$P-1)*15+$key+1).'</i><img src="" all-img="'.$wximgstr.'" data-url="'.$wximg[0].'" data="'.$value["fkey"].'"><div class="info"><span>'.$fvip_webcnickname.'</span><span class="votecount">'.$fvitem_count.'票</span></div><div>NO.1'.sprintf("%04s", $value["fkey"]).'</div><span class="btn do_vote" data="'.$value["fkey"].'">立即投票</span></div><input type="hidden" name="xuanyan" value="'.$fvitem_xuanyan.'"/>';
				}else{
					$htmlstr.='<li class="item"><div class="box"><img src="" data-url="'.$wximg[0].'" all-img="'.$wximgstr.'" data="'.$value["fkey"].'"><div class="info"><span>'.$fvip_webcnickname.'</span><span class="votecount">'.$fvitem_count.'票</span></div><div>NO.1'.sprintf("%04s", $value["fkey"]).'</div><span class="btn do_vote" data="'.$value["fkey"].'">立即投票</span></div><input type="hidden" name="xuanyan" value="'.$fvitem_xuanyan.'"/>';
				}
				
			}

			$htmlstr.='<input type="hidden" name="order" value="'.$order.'"></li>';
			$ress['page']=$show;
			$ress['htmlstr']=$htmlstr;
		if($this->action=='page'){
			echo $htmlstr.'|'.$show;
			//$this->ajaxReturn($ress,'JSON');
		}else{

			$is_reg=is_reg($this->user_id);

			$Vote = M('vote');
			$where['fkey']=$vote_id;
			$res=$Vote->where($where)->order('fkey desc')->find();
			$res['fvote_bm_count']=(int)$res['fvote_bm_count']+$res['fvote_xnbms'];
			$res['fvote_vote_count']=(int)$res['fvote_vote_count']+$res['fvote_xntps'];
			$res['fvote_scan_count']=(int)$res['fvote_scan_count']+$res['fvote_xncheck'];

			$Vote->where($where)->setInc('fvote_scan_count');



			$this->assign('page',$ress['page']);
			$this->assign('htmlstr',$ress['htmlstr']);
			$this->assign('vote',$res);
			$this->assign('moban',$moban_id);
			$this->assign('voteid',$vote_id);
			$this->assign('order',$order);
			$this->assign('dltoken',$dltoken);
			$this->assign('sub_fvipkey',$this->user_id);
			$this->assign('is_reg',$is_reg);  
			return $this->fetch('Interact/tpl/vote_tpl_'.$moban_id);
		}
		
	}
	public function vote_baoming(){
		$moban_id=I('get.moban');
		$vote_id=(int)I('get.voteid');
		$dltoken=I('get.dltoken',$_SESSION['dltoken']);
		
			//var_dump($signPackage);
			$Vote = M('vote');
			$res=$Vote->where(array('fkey'=>$vote_id))->order('fkey desc')->find();
			$this->assign('vote',$res);
			$this->assign('moban',$moban_id);
			$this->assign('voteid',$vote_id);
			$this->assign('dltoken',$dltoken);
			return $this->fetch('Interact/tpl/vote_tpl_baoming_'.$moban_id);

	}
	public function cdresse(){
			$moban_id=I('get.moban');
			$vote_id=(int)I('get.voteid');
			
			$this->assign('moban',$moban_id);
			$this->assign('voteid',$vote_id);
			return $this->fetch('Interact/tpl/cdresse');
	}
	public function cdresse_baoming(){
		    $isbm=0;
			$moban_id=I('get.moban');
			$vote_id=(int)I('get.voteid');
			$Vote = M('vote');
			$match=M('cdresse_match')->field('fmatch_img,fkey')->where(array('fvip_key'=>$this->user_id,'fvote_key'=>$vote_id))->select();
			$res=$Vote->where(array('fkey'=>$vote_id))->order('fkey desc')->find();
			$ree=M('vote_item')->where(array('fvip_key'=>$this->user_id,'fvote_key'=>$vote_id))->find();
			if(is_array($ree)){
			   $isbm=1;
			}
			$this->assign('vote',$res);
			$this->assign('moban',$moban_id);
			$this->assign('voteid',$vote_id);
			$this->assign('match',$match);
			$this->assign('isbm',$isbm);

			return $this->fetch('Interact/tpl/cdresse_tpl_baoming_'.$moban_id);
	}
	public function cdresse_detail(){
			$vitemid=I('get.vitemid');
			$moban_id=I('get.moban');
			$vote_id=(int)I('get.voteid');
			
			/*$ress=M('t_cdresse_match')->where(array('fmatch'=>$url))->find();
			$condi['fmatter_name']=array('in',$ress['fmatch_style']);
			$product=M()->table(array('t_cdresse_matter'=>'t0','t_item'=>'t1'))
			->field('t0.fitem_key,t1.fitem_thumbpicurl,t1.fitemname,t1.fitemstandardprice,t1.fitempromotionprice')
			->group('t0.fitem_key')
			->where($condi)
			->where('t0.fitem_key=t1.fkey')
			->select();*/
			$urls=M('vote_item')->where(array('fkey'=>$vitemid))->getField('fvitem_imgs');
			$ress=M('vote_item')->field('fvitem_imgs')->where(array('fvote_key'=>25))->order('fvitem_count desc')->limit(10)->select();
			//var_dump(json_decode($url));
			foreach ($ress as $key => $value) {
				$wximg=json_decode($value['fvitem_imgs']);
				$product[$key]=$wximg[0];
			}
			$where['fkey']=$vote_id;
			$vote=M('vote')->where($where)->order('fkey desc')->find();
			//var_dump($this->signPackage);
			$this->assign('product',$product);
			$this->assign('vitemid',$vitemid);
			$this->assign('moban',$moban_id);
			$this->assign('voteid',$vote_id);
			$this->assign('urls',json_decode($urls));
			$this->assign('vote',$vote);
			//var_dump($this->signPackage);
			return $this->fetch('Interact/tpl/vote_detail_'.$moban_id);

	}	
	

}